<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/7/15
  Time: 15:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <%@ include file="/WEB-INF/views/common/common.jsp" %>
    <script>
        function time_dialog_product() {
            $("div[name=time_dialog_product]").dialog('open');
        }

        $(function () {
            var v;
            var row_price;
            var values;
            $('#pay_searchbox').searchbox({
                searcher: function (value, name) {
                    values = value;
                    $.post('/member/getMemByKeyInfo.do?keyInfo=' + value, function (data) {
                        v = data;
                        $("#number").val(data.cardNum);
                        $("input[name=client_number]").val(data.cardNum);
                        $("#name").val(data.name);
                        $("#level").val(data.gradename);
                        $("#integral").val(data.integral);
                    });

                    //充值记录数据
                    $('#time_datagrid').datagrid({
                        url: '/numCharge/getRecordBykeyInfo.do?keyInfo=' + value,
                        fitColumns: true,
                        pagination: true,
                        singleSelect: true,
                        columns: [[
                            {field: 'card_num', title: '会员卡号', width: 1, align: 'center'},
                            {
                                field: 'service', title: '服务名称', width: 1, align: 'center',
                                formatter: function (value, row, index) {
                                    return value.name;
                                }
                            },
                            {field: 'oldnum', title: '充前次数', width: 1, align: 'center'},
                            {field: 'addnum', title: '充值次数', width: 1, align: 'center'},
                            {field: 'newnum', title: '充后次数', width: 1, align: 'center'},
                            {
                                field: 'pay_id', title: '支付方式', width: 1, align: 'center',
                                formatter: function (value, row, index) {
                                    if (value == 9) {
                                        return '现金';
                                    }
                                    if (value == 12) {
                                        return '银行卡';
                                    }
                                    if (value == 10) {
                                        return '支付宝';
                                    }
                                    if (value == 11) {
                                        return '微信';
                                    }
                                }
                            },
                            {field: 'paybalance', title: '支付金额', width: 1, align: 'center'},
                            {field: 'paytime', title: '充值时间', width: 1, align: 'center'},
                        ]],
                        onLoadSuccess: function (data) {
                            var panel = $(this).datagrid('getPanel');
                            var tr = panel.find('div.datagrid-body tr');
                            tr.each(function () {
                                var td = $(this).children('td');
                                td.css({
                                    "border-width": "0"
                                });
                            });
                        }
                    });
                    //剩余数据
                    $('#time_datagrid_p1').datagrid({
                        url: '/member/getMemberInfoByCardNum.do?cardnum=' + value,
                        fitColumns: true,
                        singleSelect: true,
                        showHeader: false,
                        columns: [[
                            {
                                field: 'service',
                                title: '商品名称',
                                width: 1,
                                align: 'center',
                                formatter: function (value, row, index) {
                                    return value.name;
                                }
                            },
                            {field: 'totalnum', title: '累计充值', width: 1, align: 'center'},
                            {field: 'subnum', title: '剩余次数', width: 1, align: 'center'},
                            {field: 'overtime', title: '到期时间', width: 1, align: 'center'}
                        ]],
                        onLoadSuccess: function (data) {
                            var panel = $(this).datagrid('getPanel');
                            var tr = panel.find('div.datagrid-body tr');
                            tr.each(function () {
                                var td = $(this).children('td');
                                td.css({
                                    "border-width": "0"
                                });
                            });
                        }
                    });
                },
                prompt: '请输入卡号,电话'
            });
            //选择商品数据
            $('#time_datagrid_product').datagrid({
                url: '/product/list.do',
                fitColumns: true,
                pagination: true,
                singleSelect: true,
                columns: [[
                    {field: 'sn', title: '服务序号', width: 1, align: 'center'},
                    {field: 'name', title: '服务名称', width: 1, align: 'center'},
                    {field: 'salePrice', title: '服务价格', width: 1, align: 'center'}
                ]],
                onLoadSuccess: function (data) {
                    var panel = $(this).datagrid('getPanel');
                    var tr = panel.find('div.datagrid-body tr');
                    tr.each(function () {
                        var td = $(this).children('td');
                        td.css({
                            "border-width": "0"
                        });
                    });
                },
                onDblClickRow: function (index, row) {
                    $("i[name=total]").html(row.price);
                    row_price = $("i[name=total]").html();
                    $(".sn").val(row.sn);
                    $(".name").val(row.name);
                    $("div[name=time_dialog_product]").dialog('close');
                }
            });
            $("input[name=times]").click(function () {
                var $ipnut_name_product = $(".name").val();
                if (!$ipnut_name_product) {
                    $.messager.alert('温馨提示', '请先选择商品');
                    return;
                }
            });
            //充值次数改变事件
            $("input[name=times]").change(function () {
                var num = $("input[name=times]").val();
                $("i[name=total]").html(row_price * num);
                var val = $("i[name=total]").html();
                $("input[name=outcome]").val(val);
            });
            //确定按钮
            $("#pay_btn_save").click(function () {
                if (!v) {
                    $.messager.alert('温馨提示', '请获取会员信息', 'error');
                    return;
                }
                var number = '<b style="color: red">' + '卡号' + ' : ' + v.number + ' , ' + '</b>';
                var name = '<b style="color: red">' + '姓名' + ' : ' + v.name + '</b>';
                var times = $("input[name=times]").val();
                if (!times) {
                    $.messager.alert('温馨提示', '请输入充值次数');
                    return;
                }
                var t = '<b style="color: red">' + '充值' + ' : ' + times + '</b>';
                // 确定的对话框
                $.messager.confirm('温馨提示', '您确认要为' + number + name + '的会员'
                    + t + '次吗？', function (r) {
                    if (r) { // 点了是
                        //表单
                        $('#time_form').form('submit', {
                            url: '/numCharge/save.do',
                            onSubmit: function () {
                            },
                            success: function (data) {
                                data = JSON.parse(data);
                                if (data.success) { // 成功
                                    // 提示下用户
                                    $.messager.alert('温馨提示', '成功', 'info');
                                    $.get('/time/clientitem?keyword=' + values, function (data) {
                                        console.log(data);
                                        $('#time_datagrid_p1').datagrid('load', {
                                            sn: data.sn,
                                            name: data.name,
                                            total: data.total,
                                            usable: data.usable,
                                            endTime: data.endTime
                                        });
                                    });
                                    $.get('/time/timeRecord?keyword=' + values, function (data) {
                                        $('#time_datagrid').datagrid('load', {
                                            client_number: data.clientNumber,
                                            name: data.name,
                                            beforeTimes: data.beforeTimes,
                                            times: data.times,
                                            afterTimes: data.afterTimes,
                                            extype: data.extype,
                                            outcome: data.outcome,
                                            inTime: data.inTime

                                        });
                                    });
                                } else { // 失败
                                    // 不必关闭对话框
                                    $.messager.alert('温馨提示', '保存失败', 'error');
                                }
                            }
                        });
                    }
                });
            });
            //合计金额
            $("input[name=income]").change(function () {
                $("i[name=total]").html($("input[name=income]").val());
            });
            $("input[name=incomeSub]").change(function () {
                $("i[name=total]").html($("input[name=incomeSub]").val());
            });
            var myDate = new Date();
            var y = myDate.getFullYear() + 1;
            var m = myDate.getMonth() + 1;
            var d = myDate.getDate();
            myDate = y + '-' + m + '-' + d;
            $('#time_date').val(myDate);
            $("input[name=outTime]").val(myDate);
            $("input:radio[name=date]").click(function () {
                var val = $('input:radio[name=date]:checked').val();
                var myDate = new Date();
                if (val == 6) {
                    var y = myDate.getFullYear();
                    var m = myDate.getMonth() + 1 + 6;
                    if (m > 12) {
                        y = myDate.getFullYear() + 1;
                        m = m - 12;
                    }
                    var d = myDate.getDate();
                    myDate = y + '-' + m + '-' + d;
                    $('#time_date').datebox('setValue', myDate);
                    var date = $('#time_date').datebox('getValue');
                    $("input[name=outTime]").val(date);
                } else if (val == 12) {
                    var y = myDate.getFullYear() + 1;
                    var m = myDate.getMonth() + 1;
                    var d = myDate.getDate();
                    myDate = y + '-' + m + '-' + d;
                    $('#time_date').datebox('setValue', myDate);
                    var date = $('#time_date').datebox('getValue');
                    $("input[name=outTime]").val(date);
                } else if (val == 3) {
                    var y = myDate.getFullYear();
                    var m = myDate.getMonth() + 1 + 3;
                    if (m > 12) {
                        y = myDate.getFullYear() + 1;
                        m = m - 12;
                    }
                    var d = myDate.getDate();
                    myDate = y + '-' + m + '-' + d;
                    $('#time_date').datebox('setValue', myDate);
                    var date = $('#time_date').datebox('getValue');
                    $("input[name=outTime]").val(date);
                } else if (val == 1) {
                    var y = myDate.getFullYear();
                    var m = myDate.getMonth() + 1 + 1;
                    if (m > 12) {
                        y = myDate.getFullYear() + 1;
                        m = m - 12;
                    }
                    var d = myDate.getDate();
                    myDate = y + '-' + m + '-' + d;
                    $('#time_date').datebox('setValue', myDate);
                    var date = $('#time_date').datebox('getValue');
                    $("input[name=outTime]").val(date);
                }
            });
        });
    </script>
</head>
<%--点击选择商品--%>
<div name="time_dialog_product" class="easyui-dialog" title="My Dialog" style="width:400px;height:360px;"
     data-options="resizable:true,modal:true,closed:true">
    <table id="time_datagrid_product"></table>
</div>
<body>
<table width="100%">
    <tr>
        <td width="50%">
            <div id="p1" class="easyui-panel"
                 style="height:350px;padding:10px;background:#FFFFFF;"
                 data-options="closable:true,fit:true,collapsible:true">
                <p align="right">
                    <input id="pay_searchbox" name="keyword"></input>
                </p>
                <div style="font-size: 18px;margin-left: 50px">
                        <span>
                            <p>
                            <b style="margin-left: 60px;">会员卡号：<input id="number" type="text"
                                                                      style="border: 0px;background:#FFFFFF;"
                                                                      disabled="disabled" value=""/></b>
                            <b>会员姓名：<input id="name" type="text" style="border: 0px;background:#FFFFFF;"
                                           disabled="disabled" value=""/></b>
                        </p>
                        </span>
                    <p>
                        <b style="margin-left: 60px;">当前等级：<input id="level" type="text"
                                                                  style="border: 0px;background:#FFFFFF;"
                                                                  disabled="disabled" value=""/></b>
                        <b>当前积分：<input id="integral" type="text" style="border: 0px;background:#FFFFFF;"
                                       disabled="disabled" value=""/></b>
                    </p>
                </div>
                <span>
                        <table align="center" width="90%" style="margin-left: 40px">
                            <thead>
                            <tr>
                                <td>商品序号</td>
                                <td>服务名称</td>
                                <td>累计充值</td>
                                <td>剩余次数</td>
                                <td>到期时间</td>
                            </tr>
                            </thead>
                        </table>
                        <%--<table id="table_xian">
                            <hr name="xian" color="#DDDDDD"/>
                        </table>--%>
              <table id="time_datagrid_p1"></table>
                    </span>
            </div>
        </td>
        <td width="50%">
            <div id="p2" class="easyui-panel"
                 style="height:350px;padding:10px;background:#FFFFFF;"
                 data-options="closable:true,fit:true,collapsible:true">
                <div style="margin-top: 20px;font-size: 19px;margin-left: 50px;">
                    <form id="time_form" name="time_form" method="post">
                        <table>
                            <input name="client_number" type="hidden"/>
                            <p>
                                <input type="hidden" class="sn" name="commodity.sn"/>
                                选择商品:&nbsp;&nbsp;
                                <input class="name" name="commodity.name" onclick="time_dialog_product()" type='text'
                                       placeholder="点击选择商品"
                                       onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)"
                                       onblur="this.v();"/>
                            </p>
                            <p>
                                充值次数:&nbsp;&nbsp;
                                <input name="times" type='text' placeholder="请输入充值次数"
                                       onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)"
                                       onblur="this.v();"/>
                            </p>
                            <p>
                                支付方式:&nbsp;&nbsp;
                                <input type="radio" name="extype" value="8" checked/> 现金
                                <input type="radio" name="extype" value="9"/> 银行卡
                                <input type="radio" name="extype" value="10"/> 支付宝
                                <input type="radio" name="extype" value="11"/> 微信
                            </p>
                            有效时间:&nbsp;&nbsp;
                            <input type="hidden" name="outTime"/>
                            <input id="time_date" type="text" class="easyui-datebox"></input>
                            <%--<input id="validity_date" name="validity_date" class="laydate-icon" readonly type="text" placeholder="请输选择时间">--%>
                            <%--<input id="" type="text" style="border: 0px;background:#fafafa;" disabled="disabled" value=""/>--%>
                            <p>
                                &emsp;&emsp;&emsp;&emsp;&nbsp;&nbsp;&nbsp;
                                <input type="radio" name="date" value="1"/> 一个月
                                <input type="radio" name="date" value="3"/> 三个月
                                <input type="radio" name="date" value="6"/> 半年
                                <input type="radio" name="date" value="12" checked/> 一年
                            </p>
                            <p>
                                备&emsp;&emsp;注:&nbsp;&nbsp;
                                <input type="text" name="intro" name="intro" class="easyui-textbox"
                                       prompt="请输入备注,最多50个字">
                            </p>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <a id="pay_btn_save" class="easyui-linkbutton">确认</a>
                        </table>
                    </form>
                </div>
            </div>
        </td>
    </tr>
</table>
</body>
<body>
<table id="time_datagrid"></table>
</body>
</html>
